<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>openai-demo</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #app {
        width: 1000px;
        margin: 100px auto 0;
        text-align: center;
      }
      h3 {
        margin-bottom: 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h3>openai-demo</h3>
      <main>
        <span>发送：</span>
        <input type="text" id="send_msg" />
        <input type="button" id="send_msg_btn" value="发送" />
        <br />
        <span>接收：</span>
        <span id="rec_msg"></span>
      </main>
    </div>
    <script type="module">
      import { catchAwait } from "../node_modules/utils-lib-js/dist/esm/index.js";
      import { sendMsg, elems } from "./main.js";
      const { send_msg, rec_msg, send_msg_btn } = elems;
      const init = async () => {
        send_msg_btn.addEventListener("click", async () => {
          const [err, res] = await catchAwait(sendMsg(send_msg.value));
          if (err) return err;
          const { choices } = res;
          rec_msg.textContent = choices[0]?.message?.content ?? "报错了";
        });
      };

      init();
    </script>
  </body>
</html>
